<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="cfn" uri="http://com.abou/cfn"%>
<%@page import="com.abou.constant.product.*"%>
<%@page import="com.abou.mvc.controller.admin.HomeController"%>
<%@ include file="../../constants.jsp"%>
<%@ include file="../vars.jsp"%>

<%-- Creates a product div. Parameters are :
    - pageUrl : the URL page
    - productDetailUrl : the product detail URL
    - computeDetailUrl : optional, whther should compute the detail URL
--%>

<c:set var="productDetailUrl" value="${param.productDetailUrl}"></c:set>

<%-- Displaying the result of the search --%>
<c:if test="${not empty searchProductsResult}">
  <ul class="search-results-after-title">
	<%-- The label notifying the nb of lines in the result --%>
    <li>
      <c:choose>
        <c:when test="${searchProductsResult.totalRowsNumber == 0}">
          	<spring:theme code="search.result.none" />
        </c:when>
        <c:when test="${searchProductsResult.totalRowsNumber == 1}">
          	<spring:theme code="search.result.one" arguments="${searchProductsResult.totalRowsNumber}"/>
        </c:when>
        <c:otherwise>
          	<spring:theme code="search.result.many" arguments="${searchProductsResult.totalRowsNumber}"/>
        </c:otherwise>
      </c:choose>
    </li>
	</ul>

  	<%-- Displaying the table, if results exist --%>
 	<c:if test="${not empty searchProductsResult.products}">

		<%-- The paging widget container --%>
		<p id="pagingContainer">
		  <jsp:include page="/WEB-INF/jsp/common/cmp/paging.jsp">
		    <jsp:param name="pageIndex" value="${searchProductsRequest.pageIndex}"/>
		    <jsp:param name="totalRowsNb" value="${searchProductsResult.totalRowsNumber}"/>
		    <jsp:param name="nbRowsPerPage" value="${searchProductsRequest.rowsNbByPage}"/>
		    <jsp:param name="formId" value="pageIndexForm"/>
		    <jsp:param name="formParam" value="pageIndex"/>
		  </jsp:include>

		  <%-- The form used to change the page --%>
		  <form:form modelAttribute="searchProductsRequest" id="pageIndexForm" name="pageIndexForm"
		      cssStyle="display:none;"  action="${param.pageUrl}">
		    <form:hidden path="name"/>
		    <form:hidden path="productType"/>
		    <form:hidden path="description"/>
		    <form:hidden path="rowsNbByPage"/>
		    <form:hidden path="pageIndex"/>
		  </form:form>
		</p>

		<%-- Displaying the table --%>
	    <c:set var="tableId" value="productsTable"/>
	    <table cellspacing="0" class="dataTable tree" id="${tableId}">

	        <%-- The caption --%>
	        <caption>
	          <%-- The table rows number chooser --%>
	          <span id="rowsNbChooser">
	            <form:form modelAttribute="searchProductsRequest" id="tableParamsForm" name="tableParamsForm"
	                 action="${pageUrl}">
	              <label><spring:theme code='search.page.rows.nb' /></label>
	              <form:select path="rowsNbByPage" multiple="false"
	                id="rowsNbByPage" onchange="$('#tableParamsForm').submit();">
	                 <form:options items="<%=HomeController.TABLE_ROWS_NBS_PER_PAGE %>"/>
	              </form:select>
				    <form:hidden path="name"/>
				    <form:hidden path="productType"/>
				    <form:hidden path="description"/>
				    <form:hidden path="rowsNbByPage"/>
				    <form:hidden path="pageIndex"/>
	              	<input type="hidden" id="pageIndex" name="pageIndex" value="0"/>
	              	<input type="hidden" name="rowsNbModif" value="true"/>
	            </form:form>
	          </span>
	        </caption>

			<%-- The table body --%>
        	<tbody>
        		 <c:set var="startTr" value="true"></c:set>
				 <%-- products --%>
		          <c:forEach items="${searchProductsResult.products}" var="product" varStatus="productStatus">

				   <c:if test="${not empty startTr}">
						  <tr>
						  <c:set var="startTr" value=""></c:set>
					</c:if>

					<c:if test="${not empty param.computeDetailUrl}">

						<!-- computing the product details URL -->
						<c:choose>
							<c:when test="${product.productType == EProductType_CLOTHING_BOY}">
								<c:url var="productDetailUrl" value="/customer/products/boy/clothing-boy-detail.do?productId=" />
							</c:when>
							<c:when test="${product.productType == EProductType_CLOTHING_GIRL}">
								<c:url var="productDetailUrl" value="/customer/products/girl/clothing-girl-detail.do?productId=" />
							</c:when>
							<c:when test="${product.productType == EProductType_CLOTHING_MAN}">
								<c:url var="productDetailUrl" value="/customer/products/man/clothing-man-detail.do?productId=" />
							</c:when>
							<c:when test="${product.productType == EProductType_CLOTHING_WOMAN}">
								<c:url var="productDetailUrl" value="/customer/products/woman/clothing-woman-detail.do?productId=" />
							</c:when>
							<c:when test="${product.productType == EProductType_BODY_CARE}">
								<c:url var="productDetailUrl" value="/customer/products/body-care/body-care-detail.do?productId=" />
							</c:when>
							<c:when test="${product.productType == EProductType_BOOK}">
								<c:url var="productDetailUrl" value="/customer/products/book/book-detail.do?productId=" />
							</c:when>
							<c:when test="${product.productType == EProductType_ACCESSORIES}">
								<c:url var="productDetailUrl" value="/customer/products/accessories/accessories-detail.do?productId=" />
							</c:when>
						</c:choose>
					</c:if>

					<td>
						<jsp:include page="/WEB-INF/jsp/common/cmp/product.jsp">
					        <jsp:param name="productLnk" value="${productDetailUrl}${product.id}"/>
					        <jsp:param name="productName" value="${cfn:format(pageContext, product.name)}"/>
					        <jsp:param name="productImg" value="${cfn:format(pageContext, product.imagePath1)}"/>
					        <jsp:param name="productPrice" value="${cfn:format(pageContext, product.price)}"/>
					     </jsp:include>
					</td>

					<%-- 3 products in one row --%>
					<c:if test="${productStatus.count % 3 == 0}">
						  </tr>
						  <c:set var="startTr" value="true"></c:set>
					</c:if>

		          </c:forEach>
				  <%-- The empty row if no lines were found --%>
		          <c:if test="${searchProductsResult.totalRowsNumber == 0}">
		            <tr><td colspan="4" class="nospace">&nbsp;</td></tr>
		          </c:if>
        	</tbody>
		</table>

		<%-- The paging widget container --%>
	    <p id="pagingContainer">
	      <jsp:include page="/WEB-INF/jsp/common/cmp/paging.jsp">
	        <jsp:param name="pageIndex" value="${searchProductsRequest.pageIndex}"/>
	        <jsp:param name="totalRowsNb" value="${searchProductsResult.totalRowsNumber}"/>
	        <jsp:param name="nbRowsPerPage" value="${searchProductsRequest.rowsNbByPage}"/>
	        <jsp:param name="formId" value="pageIndexForm"/>
	        <jsp:param name="formParam" value="pageIndex"/>
	      </jsp:include>
	    </p>

	  	<script type="text/javascript">
      		$(document).ready(function() {
      			$("img").each(function(){
      				if(this.width > 230 || this.height > 250){
      					this.width = 230;
      					this.height = 250;
      				}
          		});
      		});
 		</script>
	</c:if>
</c:if>

